package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.AiteMapper;
import cn.wolfcode.trip.base.mapper.MessageMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IAiteService;
import cn.wolfcode.trip.base.service.IUserService;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.nio.cs.ext.MS874;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private AiteMapper aiteMapper;

    @Autowired
    private TravelMapper travelMapper;

    /**
     * 既可以判断用户注册,又可以判断 用户登录账户密码的验证,是的校验
     *
     * @param email,password;
     * @param password
     * @return
     */
    @Override
    public User getUserByEmailAndPassword(String email, String password) {
        User user = userMapper.getUserByEmailAndPassword(email, password);
        return user;
    }

    @Override
    public void save(User user) {
        userMapper.insert(user);
    }

    /**
     * 注册时检测邮箱是否存在
     *
     * @param user
     */
    public User regist(User user) {
        User temp = userMapper.getUserByEmailAndPassword(user.getEmail(), null);
        if (temp != null) {
            //用户存在
            throw new RuntimeException("用户已经存在");
        }

        //设置默认头像
        user.setHeadImgUrl("/img/user/head.jpg");
        //设置默认背景
        user.setCoverImgUrl("/img/user/bg.jpeg");
        user.setGender(2);
        //可以注册
        Long userId = Long.valueOf(userMapper.insert(user));
        //将注册的新用户查出来
        User newUser = userMapper.selectByPrimaryKey(userId);
        return newUser;


    }

    /**
     * 用户用户登录
     *
     * @param email,password
     * @return
     */
    @Override
    public User login(String email, String password) {
        User temp = userMapper.getUserByEmailAndPassword(email, password);
        if (temp == null) {
            //说明账号或者密码错误
            throw new RuntimeException("账号或者密码错误");
        }
        //登录成功
        return temp;
    }

    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public User getUser(Long userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        //将用户的关注数量设置进去
        user.setFans(travelMapper.getFansS(user.getId()));
        return user;
    }

    @Override
    public int getGreatNum(Long authorId,int state) {
        return userMapper.getGreatNum(authorId,state);
    }

    @Override
    public List<Map<String, Object>> getGreatMessage(Long authorId) {
        return userMapper.getGreatMessage(authorId);
    }

    @Override
    public List<Map<String, Object>> getCommentMessage(Long authorId) {
        return userMapper.getCommentMessage(authorId);
    }

    @Override
    public int getCommentNum(Long authorId, int state) {
        return userMapper.getCommentNum(authorId,state);
    }

    @Override
    public List<Map<String, Object>> getSyetemMessage(Long authorId) {
        /**
         * 查询系统通知
         */
        return messageMapper.selectMessageByUserId(authorId,5);
    }

    @Override
    public int getSystemNum(Long authorId, int read) {
        return messageMapper.getSystemNum(authorId,read,5);
    }

    @Override
    public int getPrivateNum(Long authorId, int read) {
        return messageMapper.selectPrivateNum(authorId,read);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public int getAiteNum(Long authorId, int read) {
        return aiteMapper.getAiteNum(authorId,read);
    }

    @Override
    public List<Map<String, Object>> getAiteMessage(Long authorId) {
        return aiteMapper.getAiteMessageByUserId(authorId);
    }
}
